<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-7-15
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */
class CategoryHasTagsDB  extends DBReader{

    //检查信息是否已经添加
    public function getCategoryHasTags($category_id,$tag_id){

        $sql = "select tag_id,category_id from category_has_tags where tag_id = :tag_id and category_id = :category_id";

        $bindarr = array(
            'tag_id'=>$tag_id,
            'category_id'=>$category_id,
        );
        return $this->db()->getAll($sql,$bindarr);
    }

    //添加 category 拥有的 tag 信息
    public function addCategoryHasTags($category_id,$tag_id){

        $sql = "insert into category_has_tags(tag_id,category_id) values(:tag_id,:category_id)";

        $bindarr = array(
            'tag_id'=>$tag_id,
            'category_id'=>$category_id,
        );
        $this->db()->execute($sql,$bindarr);
        return $this->db()->lastInsertId();
    }

    //删除 tag_id 对应的信息
    public function delByTagId($tag_id){
        $bindarr = array(
            'tag_id'=>$tag_id,
        );

        $sql = "delete from category_has_tags where tag_id = :tag_id";
        $this->db()->execute($sql,$bindarr);
    }

    //删除 category_id 对应的信息
    public function delByCategoryId($category_id){
        $bindarr = array(
            'category_id'=>$category_id,
        );

        $sql = "delete from category_has_tags where category_id = :category_id";
        $this->db()->execute($sql,$bindarr);
    }

    //删除 category_id and tag_id 对应的信息
    public function delByCategoryIdAndTagID($category_id,$tag_id){
        $bindarr = array(
            'category_id'=>$category_id,
            'tag_id'=>$tag_id
        );

        $sql = "delete from category_has_tags where category_id = :category_id and tag_id = :tag_id";
        $this->db()->execute($sql,$bindarr);
    }

    //修改当前新闻标签所属类型
    public function updateCategoryHasTags($category_id,$old_category_id,$tag_id){
        $bindarr = array(
            'category_id'=>$category_id,
            'old_category_id'=>$old_category_id,
            'tag_id'=>$tag_id
        );
        $sql = "update sources set category_id = :category_id where tag_id = :tag_id and category_id = :old_category_id ";
        $rs = $this->db()->execute($sql,$bindarr);
        return $rs->rowCount();
    }
}